package com.booter.webflux.config;

import io.r2dbc.pool.ConnectionPool;
import io.r2dbc.pool.ConnectionPoolConfiguration;
import io.r2dbc.spi.ConnectionFactories;
import io.r2dbc.spi.ConnectionFactory;
import io.r2dbc.spi.ConnectionFactoryOptions;
import io.r2dbc.spi.Option;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.r2dbc.core.DatabaseClient;
import org.springframework.data.r2dbc.core.DefaultReactiveDataAccessStrategy;
import org.springframework.data.r2dbc.core.ReactiveDataAccessStrategy;
import org.springframework.data.r2dbc.dialect.MySqlDialect;
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;

import java.time.Duration;

//https://www.jdon.com/52285
@EnableR2dbcRepositories(basePackages = {"com.booter.webflux.repository.mysql"})
@Configuration
public class DataSourceConfig {

    @Bean
    public ConnectionFactory connectionFactory() {
//        return MySqlConnectionFactory.from(MySqlConnectionConfiguration.builder()
//                .host("127.0.0.1")
//                .port(3306)
//                .username("root")
//                .password("root")
//                .database("test")
//                .build());
        ConnectionFactory connectionFactory = ConnectionFactories.get(ConnectionFactoryOptions.builder()
                .option(Option.valueOf("driver"), "pool")
                .option(Option.valueOf("protocol"), "mysql")
                .option(Option.valueOf("host"), "localhost")
                .option(Option.valueOf("user"), "root")
                .option(Option.valueOf("password"), "root")
                .option(Option.valueOf("database"), "test")
                //.option(Option.valueOf("locale"), "en_US")
                .build());
        ConnectionPoolConfiguration configuration = ConnectionPoolConfiguration.builder(connectionFactory)
                .maxIdleTime(Duration.ofMinutes(30))
                .initialSize(1)
                //指定MAX_SIZE为Integer.MAX_VALUE，否则连接池大小仍保持10
                .maxSize(30)
                .maxCreateConnectionTime(Duration.ofSeconds(1))
                .build();
        return new ConnectionPool(configuration);
    }

    @Bean("reactiveDataAccessStrategy")
    public ReactiveDataAccessStrategy dataAccessStrategy() {
        return new DefaultReactiveDataAccessStrategy(MySqlDialect.INSTANCE);
    }

    @Bean("r2dbcDatabaseClient")
    public DatabaseClient databaseClient(@Qualifier("connectionFactory") ConnectionFactory connectionFactory,
                                         @Qualifier("reactiveDataAccessStrategy") ReactiveDataAccessStrategy strategy) {
        //new DefaultReactiveDataAccessStrategy(MySqlDialect.INSTANCE, Collections.singletonList(new MyEnumTypeConverter()));
        //DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
        DatabaseClient databaseClient = DatabaseClient.builder()
                .connectionFactory(connectionFactory)
                .dataAccessStrategy(strategy)
                .build();
        return databaseClient;
    }
}
